package love.yuanmengrobot.qianyugarden.utils;

import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class AppFileUtils {

    public static AppFileUtils appFileUtils=new AppFileUtils();
    public static String bookfilePath;
    public static String imgfilePath;

    AppFileUtils() {
        //获取根目录
        try {
            File path = new File(ResourceUtils.getURL("classpath:").getPath());
            if (!path.exists()) path = new File("");
            File upload = new File(path.getAbsolutePath(), "upload/BookFile/");//书籍存放区
            File upload2 = new File(path.getAbsolutePath(), "upload/Images/");//图片存放区

            if (!upload.exists()) upload.mkdirs();
            if (!upload2.exists()) upload2.mkdirs();

            bookfilePath = upload.getAbsolutePath();
            imgfilePath = upload2.getAbsolutePath();
            System.out.println("文件存储目录创建完毕！");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public synchronized static String createBookFileName(String fileName){
        //23
        return RandomUtils.createIdOX("BF")+"-"+fileName;
    }


}